package nobu.nakata.nfcfare.resource.mapper.strategy;

import net.sf.jsr107cache.Cache;
import nobu.nakata.nfcfare.resource.item.IBusResourceItem;
import nobu.nakata.nfcfare.resource.mapper.IPersistenceStrategy;


public class BusCaching implements IPersistenceStrategy<IBusResourceItem, Long> {
	private Caching<IBusResourceItem, Long> caching;
	
	
	public BusCaching(Cache cache) {
		this.caching		= new Caching<IBusResourceItem, Long>(cache, new KeyFactory());
	}

	
	@Override
	public IBusResourceItem get(Long key) {
		return caching.get(key);
	}
	@Override
	public void set(IBusResourceItem item) {
		caching.set(item);
	}
	@Override
	public void remove(IBusResourceItem item) {
		caching.remove(item);
	}
	
	private class KeyFactory implements ICacheKeyFactory<IBusResourceItem, Long> {
		private final String KIND = IBusResourceItem.class.getSimpleName();

		@Override
		public String makeCacheKeyFromValue(IBusResourceItem item) {
			return makeCacheKey(item.getBusCode());
		}

		@Override
		public String makeCacheKey(Long key) {
			return KIND + ":" + key;
		}
	}
}
